//
//  FirstJFBMerchandiseCategoryDS.m
//  ios-supplier
//
//  Created by tc09 on 15/6/11.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "FirstJFBMerchandiseCategoryDS.h"

@interface FirstJFBMerchandiseCategoryDS()
@property (nonatomic, strong) NSMutableArray * items;
@end


@implementation FirstJFBMerchandiseCategoryDS
- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }
    self.total = [[attributes objectForKey: @"total"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"total"] integerValue];
    self.categoryName = [[attributes objectForKey: @"categoryName"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"categoryName"];

    self.categoryId = [[attributes objectForKey: @"categoryId"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"categoryId"] integerValue];
    self.categoryTreeNodeId = [[attributes objectForKey: @"categoryTreeNodeId"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"categoryTreeNodeId"] integerValue];
    self.shelvesNum = [[attributes objectForKey: @"shelvesNum"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"shelvesNum"] integerValue];
    
    return self;
}

//加载商品分类列表
+ (NSURLSessionDataTask *)loadMerchandiseCategoryList:(void (^)(NSArray *categoryList, NSError *error))block pager:(JFBPager*)pager {

    NSDictionary *dic = @{@"ut":[[NSUserDefaults standardUserDefaults] objectForKey:@"ut"],@"categoryTreeNodeId":@"0"};
    
    return [[JFBAPIClient sharedClient:true] GET:@"/back-product/queryMerchantCategoryTreeROOTNode.do" parameters:dic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"code"];
        if( [error isEqual: @"0"] ) {
            NSMutableArray *result = [@[] mutableCopy];
//            NSArray *response = [JSON valueForKeyPath:@"data"];
            id jsonData = [JSON valueForKeyPath:@"data"];
            NSArray *response = [jsonData valueForKeyPath:@"categoryList"];
            for( NSDictionary *staff in response ) {
                [result addObject:[[FirstJFBMerchandiseCategoryDS alloc] initWithAttributes:staff]];
            }
            NSLog(@"%@", result);
            
            NSLog(@"json:%@", JSON);
            block([NSArray arrayWithArray:result], nil);
        } else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
}
@end
